<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>应急调查上报信息</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" href="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="../lib/jquery.mobile-1.4.5/jquery.min.js"></script>
    <script src="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script src="../lib/config.js"></script>
    <script src="../lib/common.js"></script>
    <style>
    table {
        width: 100%;
        border-spacing: 0;
    }

    th {
        text-align: left;
    }

    th h3 {
        margin: .6em 0 .6em .5em;
    }

    th,
    td {
        vertical-align: top;
        border-top: 1px solid #eee;
        padding: 1px 3px;
        background-color: #fcfcfc;
    }

    td .ui-btn {
        margin: .4em 0 .5em 0;
    }

    td .ui-btn-inner {
        padding: .4em 15px;
    }
    </style>
</head>

<body>
    <div style="margin:5px 10px;">
        <table id="tb" class="ui-responsive table-stroke" id="table-column-toggle" data-role="table" data-mode="columntoggle" data-column-btn-text="选择列">
            <thead>
                <tr>
                    <th data-priority="1">调查点名称</th>
                    <th data-priority="1">调查日期</th>
                    <th data-priority="2">地理位置</th>
                    <!-- <th data-priority="1"></th>
                    <th data-priority="1"></th>
                    <th data-priority="1"></th> -->
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>名称</th>
                    <td>日期</td>
                    <td>位置</td>
                    <td><a href="javascript:void(0);" data-rel="external">查看</a></td>
                    <td><a href="javascript:void(0);" data-rel="external">修改</a></td>
                    <td><a href="javascript:void(0);" data-rel="external">上报</a></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
var record = {};
apiready = function() {
    readFromFile();
};

var numFields =
    'YJDC01040' +
    'YJDC01050' +
    'YJDC01061' +
    'YJDC01062' +
    'YJDC01063' +
    'YJDC01068' +
    'YJDC01069' +
    'YJDC01080' +
    'YJDC01090' +
    'YJDC01100' +
    'YJDC01110' +
    'YJDC01120' +
    'YJDC01130' +
    'YJDC01141' +
    'YJDC01142' +
    'YJDC01170' +
    'YJDC01200' +
    'YJDC01180' +
    'YJDC01181' +
    'YJDC01182' +
    'YJDC01190';

function readFromFile() {
    $("#tb tbody").html("");
    api.readFile({
        path: 'fs://dataGather.txt'
    }, function(ret, err) {
        if (ret.status) {
            var data = JSON.parse(ret.data);
            if (data == null || data.record == null || data.record == '')
                return;
            record = data.record;
            //根据时间过滤，只考虑最近7个星期的，其他的删除
            var filterRecord = [];
            for (var i = 0; i < record.length; i++) {
                var rec = record[i];
                var timeStr = record[i].YJDC01270;
                var time = new Date(timeStr);
                var timeNow = new Date();
                var days = (timeNow.getTime() - time.getTime()) / (24 * 60 * 60 * 1000);
                if (days < 7)
                    filterRecord.push(record[i]);
                for (var key in rec) {
                    if ((!rec[key]) && (numFields.indexOf(key) != -1)) {
                        rec[key] = 0;
                    }
                }
            }
            record = filterRecord;
            // showMsg('读取文件内容:' + JSON.stringify(record));
            refreshTable();
        }
    });
}

function deleteFields(item){
    if (item.pntName) {
        delete item.pntName;
    }
    if (item.hazardName) {
        delete item.hazardName;
    }
    if (item.hazardType) {
        delete item.hazardType;
    }
    if (item.hazardType) {
        delete item.hazardType;
    }
    if (item.pics) {
        delete item.pics;
    }
}

function saveRecord(item) {
    var pics = item.pics;
    // showMsg('进入saveRecord,参数' + JSON.stringify(item));
    deleteFields(item);
    // showMsg('完成字段删减,参数' + JSON.stringify(item));

    $.ajax({
            url: ip_req + 'ZHFZ/ZHFZService.svc/SaveYJDCJBQK01',
            type: 'get',
            dataType: 'jsonp',
            data: { parameters: JSON.stringify(item) },
        })
        .done(function(response) {
            // showMsg('进入done,结果:' + JSON.stringify(response));
            //发送短信通知相关人
            // sendMsg2Relation(item);
            //保存照片
            uploadPictures(pics, item.Id, 'YJDC');
            // uploadPictures(pics, item.id, 'yjdcApp');
            //删除已上报的记录，并重新保存文件
            var newRecord = [];
            for (var i = 0; i < record.length; i++) {
                if (record[i].id != item.id) {
                    newRecord.push(record[i]);
                }
            }
            data = {
                "record": newRecord
            };
            api.writeFile({
                path: 'fs://dataGather.txt',
                data: JSON.stringify(data)
            }, function(ret, err) {
                // showMsg(JSON.stringify(data));
                var status = ret.status;
                if (status) {} else {
                    var msg = '保存出错: ' + err.code + '错误信息' + err.msg;
                    showErro(mssg);
                }
            });
            readFromFile();
            showMsg('保存成功！');
        });

}
// function saveRecord(item) {
//  var data = {
//      "record" : [item]
//  };
//  addRecords('qcqf', 'YJDCJBQK01', data, function(response) {
//      //发送短信通知相关人
//      sendMsg2Relation(item);
//      //保存照片
//      uploadPictures(item.pics, item.id, 'yjdcApp');
//      //删除已上报的记录，并重新保存文件
//      var newRecord = [];
//      for (var i = 0; i < record.length; i++) {
//          if (record[i].id != item.id) {
//              newRecord.push(record[i]);
//          }
//      }
//      data = {
//          "record" : newRecord
//      };
//      api.writeFile({
//          path : 'fs://dataGather.txt',
//          data : JSON.stringify(data)
//      }, function(ret, err) {
//          var status = ret.status;
//          if (status) {
//          } else {
//              var msg = '保存出错: ' + err.code + '错误信息' + err.msg;
//              showErro(mssg);
//          }
//      });
//      readFromFile();
//      showMsg('保存成功！');
//  }, showErro, false);
// }

//发送短信通知相关人
function sendMsg2Relation(dcInfo) {
    var mobiles = getMobiles();
    if (mobiles == null || mobiles.length < 1)
        return;
    var now = getServerTime();
    if (now == null) {
        alert("获取服务器时间失败！");
        return;
    }
    var msg = "应急调查数据采集上报提醒：" + now;
    msg += "，" + dcInfo.YJDC01260;
    msg += "，" + dcInfo.YJDC01250;
    msg += "上报了应急调查数据，灾害点名称：" + dcInfo.hazardName + "，地理位置：" + dcInfo.YJDC01030;
    sendMsg(msg, mobiles);
}

//获取电话号码
function getMobiles() {
    var mobiles = [];
    var filter = "SYSLXR01220 = '1'";
    var order = 'SYSLXR01030 asc,SYSLXR01010 asc';
    var record = {
        data: [],
        total: 0
    };
    getRecordsByFilterOrder("qcqf", "SYS_LXR01", filter, order, function(response) {
        var persons = response.record;
        if (persons != null && persons.length > 0) {
            for (var i = 0; i < persons.length; i++) {
                if (persons[i].SYSLXR01060 == null || persons[i].SYSLXR01060 == '')
                    continue;
                if (mobiles.indexOf(persons[i].SYSLXR01060) >= 0)
                    continue;
                mobiles.push(persons[i].SYSLXR01060);
            }
        }
    }, showErro, false);
    return mobiles;
}

function refreshTable() {
    $("#tb tbody").html("");
    for (var i = 0; i < record.length; i++) {
        var item = record[i];
        var tr = '<tr>' + '<th>pntName</th>' + '<td>dateTime</td>' + '<td>location</td>' + '<td><a href="javascript:void(0);" data-rel="external">查看</a></td>' + '<td><a href="javascript:void(0);" data-rel="external">修改</a></td>' + '<td><a href="javascript:void(0);" data-rel="external">上报</a></td>' + '</tr>';
        tr = tr.replace('pntName', item.pntName);
        tr = tr.replace('location', item.YJDC01030);
        tr = tr.replace('dateTime', item.YJDC01270);
        $("#tb").append(tr);
    }
    //绑定点击事件
    $("#tb td").click(function() {
        var rowIdx = $(this).parent().prevAll().length;
        var cowInx = $(this).prevAll().length;
        if (rowIdx < 0 || cowInx < 3)
            return;
        //选择的记录
        // var item = record[rowIdx];
        if (cowInx == 3) {
            //点击了查看
            // showMsg('  ' + JSON.stringify(item));
            // setCookie('yjdcInfo', JSON.stringify(item));
            // setCookie();
            api.openWin({
                name: 'yjdcInfo',
                url: '../html/yjdcInfo.html'
            });
        } else if (cowInx == 4) {
            //点击了修改
            // setCookie('yjdcInfo', JSON.stringify(item));
            api.openWin({
                name: 'yjdcInfoEdit',
                url: '../html/yjdcInfoEdit.html'
            });
        } else if (cowInx == 5) {
            //点击了上报
            // showMsg('点击上报,参数' + JSON.stringify(item));
            // showSaveProgress();
            saveRecord(item);
            // api.hideProgress();
        }
    });
}
</script>

</html>
